﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNet.Business;
using DotNet.Model;
using System.Text;
using DotNet.Common;
public partial class Admin_adminProgram : basePage
{
    programClassBll pcBll = new programClassBll();
    ProgramBll programBll = new ProgramBll();
    int pagesize = 10;
    int selectIndex = 0;
    public string input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=1' />";
    private static readonly object objSync = new object();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["pcDt"] = pcBll.GetDataTable(0);
            if (Request.QueryString["action"]!=null)
            {
                string action = Request.QueryString["action"];
                switch (action.Trim())
                {
                    case "delAll":
                        delete();
                        break;
                    case "editAll":
                        edit();
                        break;
                    case "MoveAll":
                        moveUpdate();
                        break;
                    default:
                        break;
                }
            }
           
        }
        bindProgram();
    }

    #region 获得项目类别名称
    public string getProgramClass(int pcid)
    {
        string str = null;
        if (pcid == 0)
        {
            str = "<font color='red'>类别未绑定</red>";
        }
        else 
        {
            DataView dv = pcBll.GetModel(ViewState["pcDt"] as DataTable, pcid);
            foreach (DataRowView row in dv)
            {
                if (row != null)
                {
                    str = row["programClassName"].ToString();
                }
                else 
                {
                    str = "<font color='red'>所属类别丢失</red>";
                }
            }       
        }
        return str;
       
    }
    #endregion

    #region 移动
    public void moveUpdate()
    {
        string pid = Request.QueryString["pid"];

        string id=Request.QueryString["id"];
        if (pid.Contains(","))
        {
            string[] idParam = pid.Split(',');
            string[] pidParam = new string[idParam.Length];
            for (int i = 0; i <= idParam.Length - 1; i++)
            {
                pidParam[i] = idParam[i];
            }
            if (programBll.Update(pidParam, id))
            {

            }
        }
        else 
        {
            string[] programid ={ pid };
            if (programBll.Update(programid,id))
            {

            }
        }
    }
    #endregion

    #region 修改
    public void edit() 
    {
        string id = Request.QueryString["id"];
        if (id.Contains(","))
        {
            lock (objSync)
            {
                string[] idParam = id.Split(',');
                for (int i = 0; i < idParam.Length; i++)
                {
                    Program program = new Program();
                    program.programId =Convert.ToInt32(idParam[i]);
                    if (!CommUtil.IsNumStr(Request.Form["programSort" + idParam[i] + ""].ToString().Trim()) || Request.Form["programSort" + idParam[i] + ""].ToString().Trim() == "")
                    {
                        return;
                    }
                    if (Request.Form["programName" + idParam[i] + ""].ToString().Trim() == "")
                    {
                        return;
                    }
                    program.programName = Request.Form["programName" + idParam[i] + ""].ToString();
                    program.programSort = Convert.ToInt32(Request.Form["programSort" + idParam[i] + ""]);
                    program.programClassId = Convert.ToInt32(Request.Form["programClassId"+idParam[i]+""]);
                    
                    program.productID = Request.Form["product_hid" + idParam[i] + ""].ToString();
                    if (programBll.Update(program))
                    {
                       
                    }
                    else
                        return;
                }
            }
        }
        else 
        {
            Program program = new Program();
            program.programId = Convert.ToInt32(id);
            if (!CommUtil.IsNumStr(Request.Form["programSort" + id + ""].ToString().Trim()) || Request.Form["programSort" + id + ""].ToString().Trim() == "")
            {
                return;
            }
            if (Request.Form["programName" + id + ""].ToString().Trim() == "")
            {
                return;
            }
            program.programName = Request.Form["programName" +id+ ""].ToString();
            program.programSort = Convert.ToInt32(Request.Form["programSort" + id+ ""]);
            program.programClassId = Convert.ToInt32(Request.Form["programClassId" + id+ ""]);
            if (programBll.Update(program))
            {

            }
            else
                return;
        }
    }
    #endregion

    #region 删除
    public void delete() 
    {
        string programId = Request.QueryString["id"];
        if (programId.Contains(","))
        {
            string[] idParam = programId.Split(',');
            string[] programIds = new string[idParam.Length];
            for (int i = 0; i < idParam.Length; i++)
            {
                programIds[i] = idParam[i];
            }
            if (programBll.Delete(programIds))
            {

            }
        }
        else
        {
            if (programBll.Delete(Convert.ToInt32(programId)))
            {

            }
        }
    }
    #endregion

    #region 绑定
    public void bindProgram() 
    {
        if (Request.QueryString["CurrentIndex"]!=null)
        {
            _currentIndex = Convert.ToInt32(Request.QueryString["CurrentIndex"]);
        }
        if (Request.QueryString["programClassId"]!=null)
        {
            _programclassid = Convert.ToInt32(Request.QueryString["programClassId"]);
            selectIndex = _programclassid;
        }
        DataTable dt = programBll.pagingForProgram(_currentIndex, pagesize, _programclassid);
        if (dt.Rows.Count==0&&_currentIndex>1)
        {
            _currentIndex = _currentIndex - 1;
            dt = programBll.pagingForProgram(_currentIndex, pagesize, _programclassid);
        }
        input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=" + _currentIndex + "&programClassId=" + _programclassid+"'/>";
        this.Repeater1.DataSource = dt;
        this.Repeater1.DataBind();
    }
    #endregion

    #region 分页按钮
    public void htmlPaging() 
    {
        string str = null;
        int count = 0;
        count = programBll.getRecordCount(_programclassid);
        int pcount = count % pagesize == 0 ? count / pagesize : count / pagesize + 1;
        str = "页次：" + _currentIndex + "/" + pcount + "  每页" + pagesize + " 总收录数据" + count + "条";
        str += HtmlControls.pagIng(_currentIndex, pcount, "adminProgram.aspx?programClassId=" + _programclassid + "&CurrentIndex", "pageaa", "pagecurrent");
        Response.Write(str);
    }
    #endregion

    #region 自定义下拉框
    public string programClassDp()
    {
        StringBuilder str = new StringBuilder();       
        str.Append("<option value='0'>---顶级分类---</option>");
        str.Append(AddItems(0, 0, new StringBuilder()));       
        return str.ToString();

    }

    public string AddItems(int ParentID, int length, StringBuilder sb)
    {
        DataView dvTree = pcBll.GetDataView(ViewState["pcDt"] as DataTable, ParentID);
        length++;
        foreach (DataRowView row in dvTree)
        {
            string str = addtab(length);
            if (ParentID == 0)
            {
                sb.AppendFormat("<optgroup style=\"color:Red;\" label='" + row["programClassName"] + "'>{0}</optgroup>", str + "|--" + row["programClassName"]);
            }
            else
            {
                if (selectIndex == Convert.ToInt32(row["programClassId"]))
                {
                    sb.AppendFormat("<option selected='selected' value='" + row["programClassId"].ToString() + "'>{0}</option>", str + "|--" + row["programClassName"].ToString());
                }
                else 
                {
                    sb.AppendFormat("<option value='" + row["programClassId"].ToString() + "'>{0}</option>", str + "|--" + row["programClassName"].ToString());
                }
            }
            AddItems(Convert.ToInt32(row["programClassId"]), length, sb);
        }
        return sb.ToString();

    }

    private string addtab(int i)
    {
        string mark = "&nbsp;";
        if (i != 1)
        {
            for (int j = 0; j < i - 1; j++)
            {

                mark += "&nbsp;&nbsp";
            }
        }
        return mark;
    }
    #endregion

    #region 状态维护
    private int _programclassid = 0;
    private int _currentIndex = 1;
    #endregion

}
